package com.edu.demo.error;


/*
 * 参数全局异常拦截
 * */

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.edu.demo.error.costom.PassWordException;
import com.edu.demo.error.costom.PhoneNumberAlreadyExistsException;
import com.edu.demo.error.costom.UserNameException;
import com.edu.demo.result.Result;
import org.quartz.ObjectAlreadyExistsException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.stream.Collectors;

@RestController
@ControllerAdvice
public class ParameterError {

    //捕获空指针异常
    @ExceptionHandler(NullPointerException.class)
    public String nullPointerException() {
        return "空指针异常";
    }

    //参数校验异常拦截
    @ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    public ResponseEntity<Result<String>> handleValidatedException(Exception e) {
        Result<String> result = null;
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            result = Result.error(1000, "参数校验异常",
                    ex.getBindingResult().getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.toList())
            );
        } else if (e instanceof ConstraintViolationException) {
            ConstraintViolationException ex = (ConstraintViolationException) e;
            result = Result.error(1000, "参数校验异常",
                    ex.getConstraintViolations().stream()
                            .map(ConstraintViolation::getMessage)
                            .collect(Collectors.toList())
            );
        } else if (e instanceof BindException) {
            BindException ex = (BindException) e;
            result = Result.error(1000, "参数校验异常",
                    ex.getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.toList())
            );
        }
        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(PhoneNumberAlreadyExistsException.class)
    public Result phoneNumberAlreadyExistsException(){
        return Result.error().code(1006).msg("手机号已被注册");
    }

    @ExceptionHandler(UserNameException.class)
    public Result userNameException(){
        return Result.error().code(1006).msg("账号已被注册");
    }

    @ExceptionHandler(PassWordException.class)
    public Result passWordException(){
        return Result.error().code(1001).msg("用户名或密码错误");
    }

    //TODO 后面换为401
    @ExceptionHandler(NotLoginException.class)
    public Result notLoginException(){
        return  Result.error().code(1007).msg("未能读到有效Token");
    }

    @ExceptionHandler(NotRoleException.class)
    public Result notRoleException(NotRoleException notRoleException){
        return Result.error().code(1008).msg(notRoleException.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public Result notPermissionException(NotPermissionException notPermissionException){
        return Result.error().code(1009).msg(notPermissionException.getMessage());
    }

    @ExceptionHandler(ObjectAlreadyExistsException.class)
    public Result exception(ObjectAlreadyExistsException e){
        return Result.error().code(1010).msg("Job名重复");
    }
}